import { mapGetters } from 'vuex';
import waves from "@/directive/waves"; // Waves directive
// components
import StructsTree from '@/business-components/structs-tree/structs-tree'
import Pagination from "@/components/Pagination";
// provides
import { getTreeData } from '@/utils/utils';
import { getUserListApi, getSysStructsTreeApi } from '@/views/personnel-manage/user-manage/user-manage.service'

export default {
  name: 'view-authorized-personnel',
  components: { Pagination, StructsTree },
  directives: { waves },
  props: {
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      file_url: process.env.VUE_APP_FILE_URL, // 头像url
      loading: false, // table loading
      showDialog: true, // 是否显示弹窗
      data: [],
      recordNum: 10, // 分页组件默认的页数, 分页里的select默认10, 20, 50
      totalNum: 0, // 数据总数
      pageNum: 1, // 页数
      title: '查看职位授权人员',
      structuresTreeData: [], // 组织架构
      defaultProps: { // 部门树变量
        children: "children",
        label: "name"
      },
      sysStructId: ''
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getSysStructsTree(); // 获取组织架构
    this.getDataList();
  },
  methods: {
    setIndex(index) {
      return (this.pageNum - 1) * this.recordNum + index + 1;
    },
    // 获取
    getDataList() {
      const params = {
        roleIds: [this.roleId],
        recordNum: this.recordNum,
        pageNum: this.pageNum
      }
      if (this.sysStructId) {
        params.structIds = [this.sysStructId]
      }
      getUserListApi(params).then(res => {
        this.data = res.data;
        this.totalNum = Number(res.dataCount);
      })
    },

    // 获取部门树
    getSysStructsTree() {
      getSysStructsTreeApi().then(res => {
        this.structuresTreeData = getTreeData(res.data);
      })
    },

    handleNodeClick(data) {
      this.sysStructId = data.objectId;
      this.getDataList();
    },

    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },

    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.showDialog = false;
      this.$emit('close', true);
    }
  }
}
